import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn import svm

CENTERS = 2
fig,axs = plt.subplots(1,3,figsize=(15,4))

def plot(clf, X, C, ax):
    axs[ax].scatter(clf.support_vectors_[:,0], clf.support_vectors_[:,1],
                    marker='o', edgecolor='r', color = 'y',
                    s=140,label='support vector')
    for target, color in zip(range(CENTERS),['c','k']):
        axs[ax].scatter(X[y==target,0], X[y==target,1], marker='^', 
                        color = color, label='class '+format(target))    
    plot_line(clf.coef_[0],clf.intercept_,X)
    axs[ax].set_xlabel('Feature 1', fontsize=15)
    if ax == 0:
        axs[ax].set_ylabel('Feature 2', fontsize=15)
    axs[ax].set_title('Soft SVM, C =' + str(C))
    axs[ax].legend()
    return

def plot_line(slope, intercept, X):
    min_x = int(min(X[:,0]))
    max_x = int(max(X[:,0]))
    x = range(min_x,max_x)
    y = -(x*slope[0] + intercept)/slope[1]   
    axs[ax].plot(x,y, color='black')
    return

X,y = make_blobs(n_samples=200,n_features=2,centers=CENTERS,
                 random_state=14, cluster_std=2.5)

for C, ax in zip([1, 0.01, 0.001], range(3)):
    clf = svm.SVC(kernel='linear', C = C)
    clf.fit(X, y) 
    plot(clf,X,C,ax)
plt.show()
